package sqlanalysisgui.view;

import org.apache.log4j.Logger;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.part.ViewPart;

import sqlanalysisgui.view.handler.ProjectClickHandler;
import sqlanalysisgui.view.handler.ProjectDbClickHandler;
import sqlanalysisgui.view.model.ProjectInfo;
import sqlanalysisgui.view.model.provider.ProjectInfoModelProvider;
import sqlanalysisgui.view.model.sorter.ProjectListViewSorter;

public class ProjectListView extends ViewPart {
	private Logger logger =  Logger.getLogger(this.getClass());
	public static final String ID = "sqlanalysisgui.view.ProjectListView"; //$NON-NLS-1$
	private Table table;
	private TableViewer tableViewer;
	public ProjectListView() {
	}

	/**
	 * Create contents of the view part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		createTable(container);
		
		createActions();
		initializeToolBar();
		initializeMenu();
	}
	
	private void createTable(Composite container){
		tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();

		table.setHeaderVisible(true);
		table.setLinesVisible(true);
	
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.setAlignment(SWT.LEFT);
		tblclmnNewColumn.setWidth(96);
		tblclmnNewColumn.setText("프로젝트");
		tblclmnNewColumn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				((ProjectListViewSorter) tableViewer.getSorter()).doSort(ProjectListViewSorter.COLUMN_PROEJCT_NAME);
				tableViewer.refresh();
			}
		});
		tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				ProjectInfo p = (ProjectInfo) element;
				return p.getProjectName();
			}
		});

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_1.setAlignment(SWT.CENTER);
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("날짜");
		tblclmnNewColumn_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				((ProjectListViewSorter) tableViewer.getSorter()).doSort(ProjectListViewSorter.COLUMN_PROEJCT_REG_DATE_NAME);
				tableViewer.refresh();
			}
		});
		tableViewerColumn_1.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				ProjectInfo p = (ProjectInfo) element;
				return p.getProjectRegDate();
			}
		});

		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_2.getColumn();
		tblclmnNewColumn_2.setAlignment(SWT.LEFT);
		tblclmnNewColumn_2.setWidth(114);
		tblclmnNewColumn_2.setText("디렉토리");
		
		tblclmnNewColumn_2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				((ProjectListViewSorter) tableViewer.getSorter()).doSort(ProjectListViewSorter.COLUMN_PROEJCT_PATH_NAME);
				tableViewer.refresh();
			}
		});
		tableViewerColumn_2.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				ProjectInfo p = (ProjectInfo) element;
				return p.getProjectPath();
			}
		});
	
		
		tableViewer.setContentProvider(new ArrayContentProvider());
	    // Get the content for the viewer, setInput will call getElements in the
	    // contentProvider
		//tableViewer.setInput(ProjectInfoModelProvider.INSTANCE.getProjectInfos());
		setDataRefresh();
		tableViewer.setSorter(new ProjectListViewSorter());
	    // Make the selection available to other views
	    getSite().setSelectionProvider(tableViewer);
	    // Set the sorter for the table
	    
	    hookCommand();
	    
	    tblclmnNewColumn.pack();
		tblclmnNewColumn_1.pack();
		tblclmnNewColumn_2.pack();
		
		((ProjectListViewSorter) tableViewer.getSorter()).doSort(ProjectListViewSorter.COLUMN_PROEJCT_REG_DATE_NAME);
		((ProjectListViewSorter) tableViewer.getSorter()).doSort(ProjectListViewSorter.COLUMN_PROEJCT_REG_DATE_NAME);
		tableViewer.refresh();
	}
	
	public void setDataRefresh(){
		tableViewer.setInput(ProjectInfoModelProvider.INSTANCE.getProjectInfos());
	}
	
	private void hookCommand(){
		hookClickCommand();
		hookDoubleClickCommand();
	}
	
	/**
	 * 	클릭 이벤트
	 * */
	private void hookClickCommand() {
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				logger.debug("프로젝트 클릭 이벤트 발생");
				IHandlerService handlerService = (IHandlerService) getSite()
						.getService(IHandlerService.class);
				try {
					handlerService.executeCommand(
							ProjectClickHandler.ID, null);
					logger.debug("프로젝트 클릭 이벤트 발생 핸들러 연결");
				} catch (Exception ex) {
					logger.debug("프로젝트 클릭 에러: " + ex);
					throw new RuntimeException(
							ProjectClickHandler.ID + " not found");
				}
			}				
		});
	}
	
	/**
	 * 	더블클릭 이벤트
	 * */
	private void hookDoubleClickCommand() {
		tableViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				logger.debug("프로젝트 더블클릭 이벤트 발생");
				IHandlerService handlerService = (IHandlerService) getSite()
						.getService(IHandlerService.class);
				try {
					handlerService.executeCommand(
							ProjectDbClickHandler.ID, null);
					logger.debug("프로젝트 더블클릭 이벤트 발생 핸들러 연결");
				} catch (Exception ex) {
					logger.debug("프로젝트 더블클릭 에러: " + ex);
					throw new RuntimeException(
							ProjectDbClickHandler.ID + " not found");
				}
			}
		});
	}

		/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
		tableViewer.getControl().setFocus();
	}

}
